/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import model.LevelManager;
import model.LevelSpecification;

/**
 *
 * @author Misja
 */
public class LevelSelectionFrame extends javax.swing.JFrame {

    /**
     * Creates new form LevelManagerFrame
     */
    LevelManager levelManager = new LevelManager();

    public LevelSelectionFrame() {
        levelManager = new LevelManager();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnPlay = new javax.swing.JButton();
        jspLevels = new javax.swing.JScrollPane();
        lstLevels = new javax.swing.JList();
        mbMain = new javax.swing.JMenuBar();
        mNavigation = new javax.swing.JMenu();
        miHome = new javax.swing.JMenuItem();
        miManager = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Selection");
        setLocationByPlatform(true);
        setResizable(false);

        btnPlay.setText("Play");
        btnPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayActionPerformed(evt);
            }
        });

        lstLevels.setModel(new javax.swing.AbstractListModel() {
            String[] strings = levelManager.getLevelStorageFileNames();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jspLevels.setViewportView(lstLevels);

        mNavigation.setText("Navigation");

        miHome.setText("Home");
        miHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miHomeActionPerformed(evt);
            }
        });
        mNavigation.add(miHome);

        miManager.setText("Level Manager");
        miManager.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miManagerActionPerformed(evt);
            }
        });
        mNavigation.add(miManager);

        mbMain.add(mNavigation);

        setJMenuBar(mbMain);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspLevels, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnPlay)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jspLevels, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPlay)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void messageDialog(ArrayList<String> messages) {
        String message = "";
        Iterator<String> it = messages.iterator();
        while (it.hasNext()) {
            message = message + it.next() + "\t";
        }
        JOptionPane.showMessageDialog(rootPane, message);
    }

    private void messageDialog(String message) {
        JOptionPane.showMessageDialog(rootPane, message);
    }

    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed

        if (lstLevels.isSelectionEmpty()) {
            this.messageDialog("Selecteer eerst het level dat u wilt spelen");
        } else {
            LevelSpecification tempLevelSpecification = levelManager.getLevelSpecificationByName((String) lstLevels.getModel().getElementAt(lstLevels.getSelectedIndex()));
            if (tempLevelSpecification != null) {
                new DifficultySelectionFrame(tempLevelSpecification).setVisible(true);
                this.dispose();
            } else {
                this.messageDialog(this.levelManager.getMsg());
                this.levelManager.clearMsg();
            }
        }

    }//GEN-LAST:event_btnPlayActionPerformed

    private void miHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miHomeActionPerformed
        new HomeFrame().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_miHomeActionPerformed

    private void miManagerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miManagerActionPerformed
        new LevelManagerFrame().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_miManagerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LevelSelectionFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LevelSelectionFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LevelSelectionFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LevelSelectionFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LevelSelectionFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPlay;
    private javax.swing.JScrollPane jspLevels;
    private javax.swing.JList lstLevels;
    private javax.swing.JMenu mNavigation;
    private javax.swing.JMenuBar mbMain;
    private javax.swing.JMenuItem miHome;
    private javax.swing.JMenuItem miManager;
    // End of variables declaration//GEN-END:variables
}
